#include "LCDCommand.h"
#include "PluginConstants.h"

static const char* COMMAND_NAMES[]          = { "NoOp", "Clear", "ReturnHome", "SetMode", "SetDisplay","SetCursorAndDisplay","SetInterface","SetCGRAM" ,"SetDDRAM"};
static const char* MODES[]                  = { "CursorDec", "CursorInc", "ShiftOff", "ShiftOn" };
static const char* DISPLAY_MODES[]          = { "DisplayOff", "DisplayOn" ,"CursorOff", "CursorOn","BlinkOff","BlinkOn" };
static const char* CURSOR_DISPLAY_MODES[]   = { "CursorMove", "DisplayShift", "ShiftRight", "ShiftLeft"  };
static const char* INTERFACE_MODES[]        = { "4Bits", "8Bits", "1Line", "2Lines", "5x8", "5x10" };
static const char* COMMAND_FORMAT           = "%s() - 0X%02X";

static int CMD_NOOP         = 0;
static int CMD_CLR          = 1;
static int CMD_HOME         = 2;
static int CMD_MODE         = 3;
static int CMD_DISPLAY      = 4;
static int CMD_CURSOR       = 5;
static int CMD_INTERFACE    = 6;
static int CMD_SETCGRAM     = 7;
static int CMD_SETDDRAM     = 8;

static char CommandBuffer[MAX_BUFFER_SIZE];

const char* CommandNoop::GetName()
{
   return COMMAND_NAMES[CMD_NOOP];
}

const char* CommandNoop::GetText( U8 Parameter )
{
   sprintf( CommandBuffer, COMMAND_FORMAT, COMMAND_NAMES[CMD_NOOP], Parameter);
   return (const char*)&CommandBuffer;
}

const char* CommandClear::GetName()
{
    return COMMAND_NAMES[CMD_CLR];
}

const char* CommandClear::GetText( U8 Parameter )
{
   sprintf( CommandBuffer, COMMAND_FORMAT, COMMAND_NAMES[CMD_CLR], Parameter);
    return (const char*)&CommandBuffer;
}

const char* CommandHome::GetName()
{
    return COMMAND_NAMES[CMD_HOME];
}

const char* CommandHome::GetText( U8 Parameter )
{
    sprintf( CommandBuffer, COMMAND_FORMAT, COMMAND_NAMES[CMD_HOME], Parameter);
    return (const char*)&CommandBuffer;
}

const char* CommandSetMode::GetName()
{
    return COMMAND_NAMES[CMD_MODE];
}

const char* CommandSetMode::GetText( U8 Parameter )
{
    sprintf( CommandBuffer,"%s(%s,%s) 0x%02X",COMMAND_NAMES[CMD_MODE], 
        MODES[ ((Parameter >>1) & 0x01)],
        MODES[ 2 + (Parameter & 0x01)], Parameter );
    return (const char*)&CommandBuffer;
}

const char* CommandSetDisplay::GetName()
{
    return COMMAND_NAMES[CMD_DISPLAY];
}

const char* CommandSetDisplay::GetText( U8 Parameter )
{
    sprintf( CommandBuffer,"%s(%s, %s, %s) 0x%02X",COMMAND_NAMES[CMD_DISPLAY],  
        DISPLAY_MODES[ (Parameter >> 2) & 0x01], 
        DISPLAY_MODES[ 2 + ((Parameter >> 1) & 0x01)],
        DISPLAY_MODES[ 4 + (Parameter & 0x01)], Parameter );
    return (const char*)&CommandBuffer;
}

const char* CommandSetCursor::GetName()
{
    return COMMAND_NAMES[CMD_CURSOR];
}

const char* CommandSetCursor::GetText( U8 Parameter )
{
    sprintf( CommandBuffer,"%s(%s, %s) 0x%02X",COMMAND_NAMES[CMD_CURSOR], 
        CURSOR_DISPLAY_MODES[ (Parameter >>3) & 0x1],
        CURSOR_DISPLAY_MODES[ 2 + ((Parameter >> 2 ) & 0x1)],Parameter  );
    return (const char*)&CommandBuffer;
}

const char* CommandSetInterface::GetName()
{
    return COMMAND_NAMES[CMD_INTERFACE];
}

const char* CommandSetInterface::GetText( U8 Parameter )
{
    sprintf( CommandBuffer,"%s(%s, %s, %s) 0x%02X", COMMAND_NAMES[CMD_INTERFACE],
        INTERFACE_MODES[ (Parameter >> 4) & 0x01 ],
        INTERFACE_MODES[ 2 + ((Parameter >>3) & 0x01)],
        INTERFACE_MODES[ 4 + ((Parameter >>2) & 0x01)], Parameter );
    return (const char*)&CommandBuffer;
}

const char* CommandSetCGRAM::GetName()
{
    return COMMAND_NAMES[CMD_SETCGRAM];
}

const char* CommandSetCGRAM::GetText( U8 Parameter )
{
    sprintf( CommandBuffer, COMMAND_FORMAT, COMMAND_NAMES[CMD_SETCGRAM], Parameter);
    return (const char*)&CommandBuffer;
}

const char* CommandSetDDRAM::GetName()
{
    return COMMAND_NAMES[CMD_SETDDRAM];
}

const char* CommandSetDDRAM::GetText( U8 Parameter )
{
    sprintf( CommandBuffer, COMMAND_FORMAT, COMMAND_NAMES[CMD_SETDDRAM], Parameter);
    return (const char*)&CommandBuffer;
}





const char* CommandSetInterface8Bits::GetName()
{
    return COMMAND_NAMES[CMD_INTERFACE];
}

const char* CommandSetInterface8Bits::GetText( U8 Parameter )
{
    sprintf( CommandBuffer,"%s(%s, %s, ?) 0x%02X", COMMAND_NAMES[CMD_INTERFACE],
        INTERFACE_MODES[ (Parameter >> 4) & 0x01 ],
        INTERFACE_MODES[ 2 + ((Parameter >>3) & 0x01)], Parameter );
    return (const char*)&CommandBuffer;
}


const char* CommandSetDisplay8Bits::GetName()
{
    return COMMAND_NAMES[CMD_DISPLAY];
}

const char* CommandSetDisplay8Bits::GetText( U8 Parameter )
{
    sprintf( CommandBuffer,"%s(?, ?, ?) 0x%02X",COMMAND_NAMES[CMD_DISPLAY], Parameter );
    return (const char*)&CommandBuffer;
}




const char* CommandSetCursor8Bits::GetName()
{
    return COMMAND_NAMES[CMD_CURSOR];
}

const char* CommandSetCursor8Bits::GetText( U8 Parameter )
{
    sprintf( CommandBuffer,"%s(%s, ?) 0x%02X",COMMAND_NAMES[CMD_CURSOR], 
        CURSOR_DISPLAY_MODES[ (Parameter >>3) & 0x1],Parameter  );
    return (const char*)&CommandBuffer;
}


const char* CommandSetCGRAM8Bits::GetName()
{
    return COMMAND_NAMES[CMD_SETCGRAM];
}

const char* CommandSetCGRAM8Bits::GetText( U8 Parameter )
{
    U8 possibleBytes[MAX_POSSIBLE_COMBINATIONS];
    for ( int i = 0; i < MAX_POSSIBLE_COMBINATIONS; ++i )
    {
        possibleBytes[i] = (U8)( Parameter | i);
    }
    sprintf(CommandBuffer,"%s(%c,%c,%c,%c,%c,%c,%c,%c)", COMMAND_NAMES[CMD_SETCGRAM], possibleBytes[0], possibleBytes[1],possibleBytes[2],
        possibleBytes[3],possibleBytes[4],possibleBytes[5],possibleBytes[6],possibleBytes[7]);
    return (const char*)&CommandBuffer;
}

const char* CommandSetDDRAM8Bits::GetName()
{
    return COMMAND_NAMES[CMD_SETDDRAM];
}

const char* CommandSetDDRAM8Bits::GetText( U8 Parameter )
{
    U8 possibleBytes[MAX_POSSIBLE_COMBINATIONS];
    for ( int i = 0; i < MAX_POSSIBLE_COMBINATIONS; ++i )
    {
        possibleBytes[i] = (U8)( Parameter | i);
    }
    sprintf(CommandBuffer,"%s(%c,%c,%c,%c,%c,%c,%c,%c)",  COMMAND_NAMES[CMD_SETDDRAM], possibleBytes[0], possibleBytes[1],possibleBytes[2],
        possibleBytes[3],possibleBytes[4],possibleBytes[5],possibleBytes[6],possibleBytes[7]);
    return (const char*)&CommandBuffer;
}




const char* CommandNoop8Bits::GetName()
{
    return COMMAND_NAMES[CMD_NOOP];
}

const char* CommandNoop8Bits::GetText( U8 Parameter )
{
    sprintf(CommandBuffer,"[%s,%s,%s,%s]", COMMAND_NAMES[CMD_NOOP], COMMAND_NAMES[CMD_CLR],COMMAND_NAMES[CMD_HOME],COMMAND_NAMES[CMD_MODE]);
    return (const char*)&CommandBuffer;
}

